<template>
  <div class="vip-list-container">

    <!-- 搜索选项 -->
    <div class="vip-list-top">
      <el-row>
        <el-col :span="5">
          <div class="vip-list-top-col">
            <span style="padding:0 5px">ID/昵称</span>
            <el-input v-model="vipParams.searchName" placeholder="请输入ID/昵称" size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="5">
          <div class="vip-list-top-col">
            <span style="padding:0 5px">手机号</span>
            <el-input v-model="vipParams.phone" placeholder="请输入手机号" size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="5">
          <div class="vip-list-top-col">
            <span style="padding:0 5px">团长</span>
            <el-input v-model="vipParams.realname" placeholder="请输入团长" size="small"></el-input>
          </div>
        </el-col>
        <el-col :span="7">
          <div class="order-manage-top-col" style="padding-left:0">
            <el-date-picker style="width:100%"
              v-model="time"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholde="开始时间"
              end-placeholde="结束时间"
              size="small">
            </el-date-picker>
          </div>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" size="small" @click="search">搜索</el-button>
        </el-col>
      </el-row>
    </div>

    <!-- 表格 -->
    <div>
      <el-table :data="vipList" style="width:100%" stripe>
        <el-table-column type="selection" width="30px"></el-table-column>
        <el-table-column prop="id" label="ID" align="center" width="80px"></el-table-column>
        <el-table-column label="头像" align="center">
          <template slot-scope="scope" width="80px">
            <l-pic :picurl="scope.row.avatar" :size="{width:40,height:40}" :disdel="false" :disview="false"/>
          </template>
        </el-table-column>
        <el-table-column label="昵称姓名">
          <template slot-scope="scope">
            <div>
              <span>昵称：</span>
              <span>{{ scope.row.nickname }}</span>
            </div>
            <div>
              <span>团长：</span>
              <span>{{ scope.row.realname === null ? '无团长' : scope.row.realname }}</span>
            </div>
            <div>
              <span>上级：</span>
              <span>{{ scope.row.parent_name === null ? '无上级' : scope.row.parent_name }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="level_name" label="会员等级" align="center" width="80px"></el-table-column>
<!--        <el-table-column label="会员等级" align="center">-->
<!--&lt;!&ndash;          0 普通会员  1 超级会员  2 代理商 3 运营商&ndash;&gt;-->
<!--          <template slot-scope="scope">-->
<!--            <div>-->
<!--              <span v-if="scope.row.level === '0'" size="small">普通会员</span>-->
<!--              <span v-if="scope.row.level === '1'" size="small">超级会员</span>-->
<!--              <span v-if="scope.row.level === '2'" size="small">代理商</span>-->
<!--              <span v-if="scope.row.level === '3'" size="small">运营商</span>-->
<!--            </div>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="账户">
          <template slot-scope="scope">
            <div>
              <span>余额：</span>
              <span>{{ scope.row.recharge_balance }}</span>
            </div>
            <div>
              <span>积分：</span>
              <span>{{ scope.row.score }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="收件地址信息">
          <template slot-scope="scope">
            <div>
              <span>手机号：</span>
              <span>{{ scope.row.phone === null ? '暂无信息' : scope.row.phone }}</span>
            </div>
            <div>
              <span>地区：</span>
              <span>{{ scope.row.pca === null ? '暂无信息' : scope.row.pca }}</span>
            </div>
            <div>
              <span>地址：</span>
              <span>{{ scope.row.address === null ? '暂无信息' : scope.row.address }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="订单消费">
          <template slot-scope="scope">
            <div>
              <span>总金额：</span>
              <span>{{ scope.row.pay_price }}</span>
            </div>
            <div>
              <span>总订单：</span>
              <span>{{ scope.row.pay_num }}</span>
            </div>
            <div>
              <span>购物车：</span>
              <span>{{ scope.row.cart_num }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="佣金">
          <template slot-scope="scope">
            <div>
              <span>预估佣金：</span>
              <span>{{ scope.row.commission }}</span>
            </div>
            <div>
              <span>待提现佣金：</span>
              <span>{{ scope.row.withdrawable_commission }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="注册时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="danger"  plain size="mini" style="padding:3px 10px" @click="putStatus(scope.row)">{{ scope.row.status === '1' ? '拉黑' : '恢复' }}</el-button>
            <el-button type="primary" plain size="mini" style="padding:3px 10px" @click="gotoOrder(scope.row)">订单</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getVipList, putVipStatus } from '@/api/vip'
import LPic from '@/components/lPic'
import { parseTime } from '@/utils/index'

export default {
  name: 'vipList',
  components: {
    LPic
  },
  data() {
    return {

      vipList: [],//表格数据

      page: 1,//页码
      count: 1,//数据总量

      vipParams: {//获取会员的请求参数
        key: this.$store.state.app.activeApp.saa_key,
        limit: 10,
        page: 1,
        searchName: '',
        phone: '',
        realname: '',
        time: ''
      },

      time: '',

      pickerOptions: {
        shortcuts: [{
          text: '近7天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start,end]);
          }
        },
        {
          text: '近30天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start,end]);
          }
        }]
      }
    }
  },
  watch: {
    time(newTime) {
      if(newTime!==null){
        const startTime = parseTime(newTime[0],'{y}-{m}-{d} {h}:{i}:{s}');
        const endTime = parseTime(newTime[1],'{y}-{m}-{d} {h}:{i}:{s}');
        this.vipParams.time = startTime + ' - ' + endTime;
      }else{
        this.vipParams.time = '';
      }
    }
  },
  mounted() {
    this.getVipList();
  },
  methods: {
    /**
     * 获取会员列表
     */
    getVipList() {
      const params = { key: this.$store.state.app.activeApp.saa_key, page: this.page, limit: 10 };
      getVipList(params).then(response => {
        if(response.status === 200){
          this.vipList = response.data;
          this.count = parseInt(response.count);
        }else if(response.status === 204){
          this.vipList = [];
          this.count = 1;
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 搜索
     */
    search() {
      getVipList(this.vipParams).then(response => {
        if(response.status === 200){
          this.vipList = response.data;
          this.count = parseInt(response.count);
        }else if(response.status === 204){
          this.vipList = [];
          this.count = 1;
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 拉黑 恢复
     */
    putStatus(data) {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        id: data.id,
        status: data.status === '1' ? '0' : '1'
      };
      putVipStatus(params).then(response => {
        if(response.status === 200){
          this.$message.success('修改成功！');
          this.getVipList();
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 跳转到订单
     */
    gotoOrder(data) {
      this.$router.push({ path:'/order/manage?id='+data.id })
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getVipList();
    }
  },
}
</script>

<style lang="scss" scoped>
.vip-list-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
.vip-list-top{
  margin: 20px auto;
  text-align: center;
  height: 40px;
  line-height: 40px;
  &-col{
    padding: 0 15px;
    max-width: 95%;
    overflow: hidden;
    display: flex;
    &>span{
      flex: 2;
      display: inline-block;
      padding: 0 5px;
      white-space: nowrap;
      overflow: hidden;
    }
    &>div{
      flex: 4
    }
  }
}
</style>
